// pages/memberManage/memberManage.js
import http from '../../utils/http.js';
import Dialog from '@vant/weapp/dialog/dialog';

import {
  saleList,deleteSales
} from "../../utils/api.js"

var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    isAdmin:null,
    storeInfo:{},
    memberList :[],
    listQuery: {
      keyword: '',
      page: 1,
      pageSize: 40,
      businessId:'',
      type:'store'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"options")
    this.setData({
      id: options.id,
      'listQuery.businessId':options.id,
      isAdmin: options.admin == 'true' ? true : false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.GetMemberList()
  },

  GetMemberList() {

    saleList(this.data.listQuery).then(res=>{
      this.setData({
        memberList: res.data.data,
        // memberList: []
      })
    })
    // let that = this
    // http.post('/api/store/v1/user/list', {
    //   id: that.data.id
    // }).then(res => {
    //   if (res.code === 200) {
    //     if (res.data.length > 0) {
    //       that.setData({
    //         memberList: res.data
    //       })
    //     }
    //   } else {
    //     wx.showToast({
    //       title: res.message,
    //       icon:'none'
    //     })
    //   }
    // })
  },
  jumpDetail(e){
    wx.navigateTo({
      url: '/pages/saleDetail/saleDetail?id=' + e.currentTarget.dataset.id
    })
  },

  JumpAddMember() {
    if (this.data.isAdmin) {
      wx.navigateTo({
        url: '/pages/Addsales/Addsales?businessId=' + this.data.id
      })
    } else {
      wx.showToast({
        title: '非管理员，暂无权限', 
        icon:'none'
      })
    }
  },

  BindMemberRemove(e) {
    Dialog.confirm({
      title: '提示',
      message: '您确定要删除这个销售嘛',
    }).then(() => {
      deleteSales({id:e.currentTarget.dataset.id}).then(res=>{
        if (res.code === 200) {
          wx.showToast({
            title: '删除成功',
            icon:'none'
          })
          this.GetMemberList()
        } else {
          wx.showToast({
            title: res.message,
            icon:'none'
          })
        }
      })
    })
  },
  // 修改
  JumpModify(e) {
    const { info } = e.currentTarget.dataset

    let data={
      name:info.name,
      phone:info.phone,
      profitRate:info.profitRate,
      id:info.id
    }
    wx.navigateTo({
      url: '/pages/Addsales/Addsales?info='
      + JSON.stringify(data)
    })
  }
})